﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;
using BLL;
using Model;

public partial class groupEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Util.UtilWeb.CheckRights(this);
            string strId = Request.QueryString["id"];
            LoadView();
            if (!string.IsNullOrWhiteSpace(strId))
                LoadView(int.Parse(strId));
        }
    }

    private void LoadView()
    {
        var lbl = Master.FindControl("lblTitle") as Label;
        lbl.Text = "团队管理";
        var list = new BllGroup().GetList(c => true);

    }

    private void LoadView(int id)
    {
        hdId.Value = id.ToString();
        btnAdd.Text = "修改";
        BllGroup bll = new BllGroup();
        var obj = bll.GetSingle(id);
        txtNo1.Text = obj.no1;
        txtNo2.Text = obj.no2;
        txtNo3.Text = obj.no3;
        txtNo4.Text = obj.no4;
        hdSmall.Value = obj.src;
        imgSmall.Src = "../" + hdSmall.Value;
        imgSmall.Visible = true;
        hdId.Value = obj.id.ToString();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string cmd = btnAdd.Text.Trim();
        BllGroup bll = new BllGroup();
        string no1 = txtNo1.Text;
        string no2 = txtNo2.Text;
        string no3 = txtNo3.Text;
        string no4 = txtNo4.Text;


        if (no1 == "")
        {
            Util.UtilWeb.Message(this,"至少输入一项", null);
            return;

        }

        if (cmd == "添加")
        {
            tb_group obj = new tb_group();
            obj.src = hdSmall.Value;
            obj.no1 = no1;
            obj.no2 = no2;
            obj.no3 = no3;
            obj.no4 = no4;
            if (bll.Add(obj))
            {
                Response.Redirect("groupList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
        if (cmd == "修改")
        {
            int id = int.Parse(hdId.Value);
            tb_group obj = bll.GetSingle(id);
            obj.src = hdSmall.Value;
            obj.no1 = no1;
            obj.no2 = no2;
            obj.no3 = no3;
            obj.no4 = no4;
            if (bll.Update(obj))
            {
                Response.Redirect("groupList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
    }

    protected void btnUpload1_Click(object sender, EventArgs e)
    {
        string path = Util.UtilWeb.UploadFile(this, fu, null, "../img/pic/");
        hdSmall.Value = path.Replace("../", "");
        imgSmall.Src = "../" + hdSmall.Value;
        imgSmall.Visible = true;
    }
}